package com.example.wangzh.developkit.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.text.Layout;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

import com.wzh.advancelog.log.WzhLog;

/**
 * @author: zhwang37
 * @date: 2021/9/15 19:11
 * @email:zhwang37@iflytek.com
 * @describe:
 */
@SuppressWarnings({"unused", "RedundantSuppression"})
@SuppressLint("DefaultLocale")
public class TextViewUsedByAnalyze extends AppCompatTextView {
    public static final String TAG = TextViewUsedByAnalyze.class.getSimpleName();
    private Paint mPaint;
    private final Path path = new Path();

    public TextViewUsedByAnalyze(Context context) {
        super(context);
        init(context, null, 0);
    }

    public TextViewUsedByAnalyze(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TextViewUsedByAnalyze(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        mPaint = new Paint();
        post(new Runnable() {
            @Override
            public void run() {
                Layout layout = getLayout();
                int linCount = layout.getLineCount();
                if (linCount < 2) {
                    return;
                }
                for (int i = 0; i < linCount; i++) {
                    Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
                    int lineBaseline = layout.getLineBaseline(i);
                    int lineTop = layout.getLineTop(i);
                    int lineBottom = layout.getLineBottom(i);
                    int lineAscent = lineBaseline + layout.getLineAscent(i);
                    int lineDescent = lineBaseline + layout.getLineDescent(i);
                    Rect lineRect = new Rect();
                    layout.getLineBounds(i, lineRect);
                    int leading = (int) fontMetrics.leading;
                    int fmTop = (int) (lineBaseline + fontMetrics.top);
                    int fmBottom = (int) (lineBaseline + fontMetrics.bottom);
                    int fmAscent = (int) (lineBaseline + fontMetrics.ascent);
                    int fmDescent = (int) (lineBaseline + fontMetrics.descent);
                    WzhLog.i(TAG, String.format("第%d行\nlineTop:%d\tlineBottom:%d\tlineAscent:%d\tlineDescent:%d\tlineBaseline:%d\nfm  Top:%d\tfm  Bottom:%d\tfm  Ascent:%d\tfm  Descent:%d\tleading:%d", i, lineTop, lineBottom, lineAscent, lineDescent, lineBaseline, fmTop, fmBottom, fmAscent, fmDescent, leading));
                }
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Layout layout = getLayout();
        int linCount = layout.getLineCount();
        if (linCount < 2) {
            return;
        }
        for (int i = 0; i < linCount; i++) {
            Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
            int lineBaseline = layout.getLineBaseline(i) + getPaddingTop();
            @SuppressLint("DrawAllocation") Rect lineRect = new Rect();
            layout.getLineBounds(i, lineRect);
            lineRect.left += getPaddingLeft();
            lineRect.right += getPaddingLeft();
            lineRect.top += getPaddingTop();
            lineRect.bottom += getPaddingTop();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.GREEN);
            path.reset();
            path.moveTo(lineRect.left, lineBaseline);
            path.lineTo(lineRect.right, lineBaseline);
            canvas.drawPath(path, mPaint);
            int fmAscent = (int) (lineBaseline + fontMetrics.ascent);
            mPaint.setColor(Color.YELLOW);
            path.reset();
            path.moveTo(lineRect.left, fmAscent);
            path.lineTo(lineRect.right, fmAscent);
            canvas.drawPath(path, mPaint);
            int fmDescent = (int) (lineBaseline + fontMetrics.descent);
            mPaint.setColor(Color.BLUE);
            path.reset();
            path.moveTo(lineRect.left, fmDescent);
            path.lineTo(lineRect.right, fmDescent);
            canvas.drawPath(path, mPaint);
            mPaint.setColor(Color.RED);
            canvas.drawRect(lineRect, mPaint);
        }
    }
}
